package com.hngy.xpq.android_practice.ui.home;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hngy.xpq.android_practice.OrderActivity;
import com.hngy.xpq.android_practice.R;
import com.hngy.xpq.android_practice.assets.XpqIcoMoon;
import com.hngy.xpq.android_practice.assets.XpqIcoMoonUtils;
import com.hngy.xpq.android_practice.bean.Comm;
import com.hngy.xpq.android_practice.bean.Orders;
import com.hngy.xpq.android_practice.bean.Wallet;
import com.hngy.xpq.android_practice.database.SQLiteComm;
import com.hngy.xpq.android_practice.database.SQLiteOrder;
import com.hngy.xpq.android_practice.database.SQLiteRecording;
import com.hngy.xpq.android_practice.database.SQLiteWallet;
import com.hngy.xpq.android_practice.http.map.BaiDuMap;
import com.hngy.xpq.android_practice.http.TcpClient;
import com.hngy.xpq.android_practice.merchant_main.Comm.Comm_Make;
import com.hngy.xpq.android_practice.merchant_main.Comm.Comm_query;
import com.hngy.xpq.android_practice.utils.WalletOrder;

import java.util.List;

public class User_Comm extends AppCompatActivity {

    ImageView image;
    TextView money,name;
    Button like,buy,comm_shop,position,make;
    SQLiteOrder sqLiteOrder;
    SQLiteComm sqLiteComm;
    SQLiteWallet sqLiteWallet;
    SQLiteRecording sqLiteRecording;
    SharedPreferences comm,main;
    Orders o;
    Wallet w;
    Comm c;
    List<Comm> lists;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_comm);

        image = findViewById(R.id.user_comm_image);
        money = findViewById(R.id.user_comm_money);
        name = findViewById(R.id.user_comm_name);
        buy = findViewById(R.id.user_comm_buy);
        like = findViewById(R.id.user_comm_like);
        comm_shop = findViewById(R.id.comm_shop);
        make = findViewById(R.id.comm_make);
        position = findViewById(R.id.user_comm_position);

        comm = super.getSharedPreferences("comm",MODE_PRIVATE);
        main = super.getSharedPreferences("main",MODE_PRIVATE);

        //详细信息的再搜索
        showQueryComm();
        //图片文字映射
        showUserAssets();


        //店铺位置显示
        position.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(User_Comm.this, BaiDuMap.class);
                startActivity(intent);
            }
        });
        //喜欢添加
        like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = sqLiteRecording.queryIdAndName(c.getComm_name());
                if (sqLiteRecording.updateLike(id ,"like")){
                    like.setTextColor(0xFFEF5252);
                } else {
                    Toast.makeText(User_Comm.this,"收藏失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
        //店铺进入方式
        comm_shop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(User_Comm.this, Comm_query.class);
                intent.putExtra("quan","no");
                startActivity(intent);
            }
        });
        //菜品制作方法展示
        make.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(User_Comm.this, Comm_Make.class);
                startActivity(intent);
            }
        });

        //图片转换
        final byte[] bytes = c.getImage();
        Bitmap b = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
        BitmapDrawable bd = new BitmapDrawable(b);

        //获得SearchComm传过来的商品信息，进行订单生成
        name.setText(c.getComm_name());
        money.setText(c.getComm_price());
        image.setImageDrawable(bd);

        //初始化数据库
        sqLiteOrder = new SQLiteOrder(this);
        sqLiteComm = new SQLiteComm(this);
        sqLiteWallet = new SQLiteWallet(this);
        sqLiteRecording = new SQLiteRecording(this);
        //购买商品，写入数据库
        buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (comm.getString("status","").equals("已售尽")){
                    Toast.makeText(User_Comm.this,"商品已下架，请等待商家重新上架!!",Toast.LENGTH_SHORT).show();
                    return;
                } else if (main.getString("name","").equals("")){
                    Toast.makeText(User_Comm.this,"请登录!!",Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(User_Comm.this, OrderActivity.class);
                    startActivity(intent);
                } else {
                    String informations = "DL001//" + main.getString("name","") + "//" + main.getString("key","");
                    String ans = new TcpClient().Transits(informations);
                    if (ans.equals("登录失败")) {
                        SharedPreferences.Editor editor = main.edit();
                        editor.putString("name","");
                        editor.putString("key","");
                        editor.commit();
                        IndexToast("账号发生改变，请重新登录");
                    } else {
                        o = new Orders();
                        o.setUser_name(main.getString("name",""));
                        o.setMerchant_name(c.getMerchant_name());
                        o.setComm_name(comm.getString("comm_name",""));
                        String inforPhone = "CX002//" + main.getString("name","");
                        String as = new TcpClient().Transits(inforPhone);
                        String[] ass = as.split("\\//");
                        o.setPhone(ass[0]);
                        o.setImage(bytes);
                        o.setComm_price(c.getComm_price());
                        o.setStatus("等待派餐");
                        o.setDes("已付款");
                        float a = Float.valueOf(new TcpClient().Transits("CX001//" + o.getUser_name()));
                        float b = Float.valueOf(comm.getString("price",""));
                        if (a > b){        //判断余额是否足够
                            w = new Wallet();
                            w.setName(main.getString("name",""));
                            w.setSingle_money("-" + c.getComm_price());
                            w.setTime(WalletOrder.getTimeDate());
                            w.setDes("购买食品:" + comm.getString("comm_name",""));
                            //数据反馈给服务端进行处理
                            String information = "DC001//" + o.getUser_name() + "//" + ass[0] + "//" + o.getComm_name() + "//" + o.getMerchant_name() + "//" + o.getComm_price() + "//" + ass[1] + "//" + o.getStatus();
                            String an = new TcpClient().Transits(information);
                            if (an.equals("账单生成成功") && sqLiteOrder.order_add(o) && sqLiteWallet.recharge(w)){
                                Toast.makeText(User_Comm.this,"点餐成功!!",Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(User_Comm.this,"系统出现故障，请稍后再试!!",Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(User_Comm.this,"钱包余额不足，请充值!!",Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        });
    }

    private void showUserAssets() {
        //喜爱收藏
        like.setText(XpqIcoMoon.LIKE);
        XpqIcoMoonUtils.setIOC(like);

        //位置显示
        position.setText(XpqIcoMoon.POSITION);
        XpqIcoMoonUtils.setIOC(position);

        //商铺
        comm_shop.setText(XpqIcoMoon.SHOP);
        XpqIcoMoonUtils.setIOC(comm_shop);

        //制作方式
        make.setText(XpqIcoMoon.MAKE);
        XpqIcoMoonUtils.setIOC(make);
    }

    private void showQueryComm(){
        c = new Comm();
        sqLiteComm = new SQLiteComm(this);
        if (lists != null){
            lists.clear();
        }
        lists = sqLiteComm.CommSearch(comm.getString("math",""));
        int a = Integer.valueOf(comm.getString("position",""));
        c = lists.get(a);
    }

    public void IndexToast(String text){
        Toast.makeText(User_Comm.this,text,Toast.LENGTH_SHORT).show();
    }
}